<template>
  <div class="role-list">
    <el-card class="box-card">
      <template #header>
        <el-form :inline="true" :model="form" class="demo-form-inline">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit()"> 查询搜索 </el-button>
            <el-button @click="onReset()"> 重置 </el-button>
          </el-form-item>
        </el-form>
      </template>
      <el-button @click="handleAdd"> 添加角色 </el-button>
      <el-table :data="roles" border style="width: 100%;">
        <el-table-column prop="id" label="编号" width="150" />
        <el-table-column prop="name" label="角色名称" width="150" />
        <el-table-column prop="description" label="描述" width="150" />
        <el-table-column prop="createdTime" label="添加时间" width="150" />
        <el-table-column label="操作" align="center" width="150">
          <template #default="scope">
            <el-button
              type="text"
              @click="$router.push({ name: 'alloc-menu', params: { roleId: scope.row.id } })"
            >
              分配菜单
            </el-button>
            <el-button type="text"> 分配资源 </el-button>
            <el-button type="text" @click="handleEdit(scope.row)"> 编辑 </el-button>
            <el-button type="text" @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogFormVisible" title="添加角色" width="50%">
      <create-or-edit
        :roleId="roleId"
        :isEdit="isEdit"
        @success="onSuccess"
        @cancel="dialogFormVisible = false"
      />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { getRoles, deleteRole } from '/@/services/Role.ts'
import CreateOrEdit from './CreateOrEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
export default defineComponent({
  name: 'RoleList',
  components: {
    CreateOrEdit
  },
  setup() {
    const form = ref({
      current: 1,
      size: 50,
      name: ''
    })
    const onSubmit = () => {
      console.log('onSubmit')
    }
    const onReset = () => {
      console.log('onReset')
    }
    const dialogFormVisible = ref(false)
    const roleId = ref(null) // 编辑角色的id
    const isEdit = ref(false) // 是编辑或添加角色
    const roles = ref([])
    const loadRoles = async () => {
      const { data } = await getRoles(form.value)
      roles.value = data.data.records
    }
    const onSuccess = () => {
      dialogFormVisible.value = false
      loadRoles()
    }
    const handleEdit = (role: any) => {
      dialogFormVisible.value = true
      roleId.value = role.id
      isEdit.value = true
    }
    const handleAdd = () => {
      isEdit.value = false
      dialogFormVisible.value = true
    }
    const handleDelete = async (role: any) => {
      try {
        await ElMessageBox.confirm(`确认删除角色：${role.name}？`, '删除提示')
        await deleteRole(role.id)
        ElMessage.success('删除成功')
        loadRoles()
      } catch (err) {
        if (err && err.response) {
          ElMessage.error('删除失败，请重试')
        } else {
          ElMessage.info('取消删除')
        }
      }
    }
    return {
      form,
      onSubmit,
      onReset,
      dialogFormVisible,
      roles,
      loadRoles,
      onSuccess,
      handleEdit,
      roleId,
      isEdit,
      handleAdd,
      handleDelete
    }
  },
  created() {
    this.loadRoles()
  }
})
</script>

<style lang="scss" scoped></style>
